# -*- coding: utf-8 -*-

import time
import MySQLdb

def GetList(ip, query) :
	conn = MySQLdb.connect(ip, "gamevil", "vlwlrpaqlf", "pz_user")
	csr = conn.cursor()

	ret_list = list()

	csr.execute(query)
	for res in csr.fetchall() :
		ret_list.append(int(res[0]))

	csr.close()
	conn.close()
	
	return ret_list

def GetCharacterSeqFromInfoItem(ip, start_pos, user_count) :
	SQL = """
		SELECT character_seq, count(seq) as cnt
		FROM info_item
		GROUP BY character_seq
		ORDER BY cnt DESC
		LIMIT %d,%d
	"""

	return GetList(ip, SQL % (start_pos, user_count))

def GetUserSeqFromDataMail(ip, start_pos, user_count) :
	SQL = """
		SELECT recv_user_seq, count(seq) as cnt
		FROM data_mail
		WHERE send_date >= %d
		GROUP BY recv_user_seq
		ORDER BY cnt DESC
		LIMIT %d,%d
	"""

	three_day_ago = int(time.time() - (3 * 3600 * 24))

	return GetList(ip, SQL % (three_day_ago, start_pos, user_count))

def GetUserSeqFromDataNews(ip, start_pos, user_count) :
	SQL = """
		SELECT my_userseq, count(*) as cnt
		FROM data_news
		WHERE regdate >= DATE_ADD(NOW(), INTERVAL -3 DAY)
		AND status = 0
		GROUP BY my_userseq
		ORDER BY cnt desc
		LIMIT %d,%d
	"""

	return GetList(ip, SQL % (start_pos, user_count))


def CallProcedureTest() :
	conn = MySQLdb.connect("172.16.3.31", "gamevil", "vlwlrpaqlf", "pz_user")
	csr = conn.cursor()

	while (True) :
		csr.callproc("sp_select_data_news", (12, 1))

		for res in csr.fetchall() :
			print res
		
		csr.close()
		csr = conn.cursor()

	try : csr.close()
	except : pass
	try : conn.close()
	except : pass

if __name__ == "__main__" :
#	print GetCharacterSeqFromInfoItem("172.16.3.31", 0)
#	print GetCharacterSeqFromInfoItem("172.16.3.31", 100)
#	print GetCharacterSeqFromInfoItem("172.16.3.31", 200)
	
#	print GetUserSeqFromDataMail("172.16.3.31", 0)
#	print GetUserSeqFromDataMail("172.16.3.31", 100)

	CallProcedureTest()

#	print GetUserSeqFromDataNews("172.16.3.31", 0)
		
